package com.nyt.org.modules.TicketStaion.service;

import com.alibaba.fastjson.JSONArray;
import com.nyt.org.common.utils.JsonConvert;
import com.nyt.org.common.utils.SendPostRequest;
import com.nyt.org.common.utils.xml.XmlUtils;
import com.nyt.org.modules.api.constant.ZhongLvCode;
import com.nyt.org.modules.api.model.XMLHead;
import com.nyt.org.modules.TicketStaion.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 售票站点数据获取 [2003]
 *
 * */
@Service
public class TicketStationGetService extends FaService{

    private Logger logger = LoggerFactory.getLogger(TicketStationGetService.class);

    @Value("${zhonglv.server}")
    private String zhonglvURL;

    public Map<String,Object> getStation(String GetCityOnly,String DynamicSite) {

        Map<String,Object> map = new HashMap<>();

        REQROOT2003 root = new REQROOT2003();

        XMLHead head = super.bulldHEADXml(ZhongLvCode.售票站点数据获取);
        root.setHead(head);

        REQBODY2003 body = new REQBODY2003();
        REQ2003 req2003 = new REQ2003();
        req2003.setGetCityOnly(GetCityOnly);
        req2003.setDynamicSite(DynamicSite);
        body.setReq2003(req2003);
        root.setBody(body);

        String xmlStr = XmlUtils.toXml(root);
        logger.info("售票站点数据获取[2003]请求报文：\n"+xmlStr);

        String resXml = SendPostRequest.doPost(zhonglvURL,xmlStr);
        logger.info("售票站点数据获取[2003]响应报文：\n"+resXml);

        // xml转bean
        RespROOT respROOT = XmlUtils.xmlToBean(resXml, RespROOT.class);

        //处理响应报文-返回到站务系统
        List<CityList> cityList =  respROOT.getBody().getRsp2003().getCityList();

        String arrayStr = JSONArray.toJSONString(cityList);

        net.sf.json.JSONArray jsonArray = JsonConvert.transToArray(arrayStr);

        map.put("rspCode",respROOT.getHead().getRspCode());
        map.put("rspMsg",respROOT.getHead().getRspMsg());
        map.put("cityList",jsonArray);
        return map;
    }

}
